package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@Api(tags = "员工管理相关接口")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    // public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO, HttpSession session) {
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //生成Token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        //将登录数据员工id存储到session会话中
        // session.setAttribute("employee",employee.getId());

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /** 
     * @description: 新增员工 
     * @param: employeeDTO 员工信息
     * @return: com.sky.result.Result 
     * @author STJ12138
     * @date: 2023/5/9 19:43
     */
    @ApiOperation("新增员工")
    @PostMapping()
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * @description: 分页查询员工
     * @param: employeePageQueryDTO 分页参数
     * @return: com.sky.result.Result<com.sky.result.PageResult>
     * @author STJ12138
     * @date: 2023/5/10 9:32
     */
    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> findAll(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询的参数：{}",employeePageQueryDTO);
        PageResult list = employeeService.findAll(employeePageQueryDTO);
        return Result.success(list);
    }

    /**
     * @description:  启用或禁用员工账号
     * @param: status 账号状态
     * @param: id 员工id
     * @return: com.sky.result.Result
     * @author STJ12138
     * @date: 2023/5/10 18:41
     */
    @ApiOperation("启用或禁用员工账号")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用或禁用员工账号：{}，{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * @description: 根据id查询员工
     * @param: id 员工id
     * @return: com.sky.result.Result<com.sky.entity.Employee>
     * @author STJ12138
     * @date: 2023/5/10 21:09
     */
    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public Result<Employee> findById(@PathVariable Long id){
        log.info("员工的id：{}",id);
        Employee employee = employeeService.findById(id);
        return Result.success(employee);
    }

    /**
     * @description: 修改员工
     * @param: employeeDTO 封装的修改信息
     * @return: com.sky.result.Result
     * @author STJ12138
     * @date: 2023/5/10 21:20
     */
    @ApiOperation("修改员工")
    @PutMapping()
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
